import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/widgets/base_appbar.dart';
import 'package:chathouse/widgets/dialog/confirm_dialog.dart';
import 'package:chathouse/widgets/form_widgets/form_widgets.dart';
import 'package:chathouse/widgets/my_input_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:get/get.dart';
import '../../constant/app_constant.dart';
import '../../model/response_bean.dart';
import '../../net/Apis.dart';
import '../../public/Obj.dart';
import '../../util/font_utils.dart';
import '../../util/toast_util.dart';

//忘記密碼
class ForgetPasswordPage extends StatefulWidget {
  const ForgetPasswordPage({Key? key}) : super(key: key);

  @override
  State<ForgetPasswordPage> createState() => _ForgetPasswordPageState();
}

class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
  bool canClick = false;
  TextEditingController textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    textEditingController.addListener(() {
      setState(() {
        canClick = textEditingController.text.isNotEmpty;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    textEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(),
      body: ListView(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "忘記密碼",
                textAlign: TextAlign.center,
                style: getTextStyle(32.sp,
                    fontType: FontType.MID, color: Colors.black),
              ).setContainer(
                  width: double.infinity, margin: EdgeInsets.only(top: 4.w)),
              Text(
                "請提供註冊的電郵地址",
                style: getTextStyle(18.sp,
                    color: Colors.black, fontType: FontType.DEFAULT),
              ).setContainer(margin: EdgeInsets.only(top: 38.w)),
              MyInputWidget(
                leftIcon: Icon(
                  Icons.email_outlined,
                  size: 24.w,
                  color: MyColors.gary_155155155,
                ).setContainer(margin: EdgeInsets.only(right: 4.w)),
                controller: textEditingController,
              ).setContainer(margin: EdgeInsets.only(top: 16.w)),
              FormWidgets.getTextButton(
                      "發送電郵",
                      AppConstant.userRole == UserRole.c
                          ? MyColors.brow_2551520
                          : MyColors.green_69157154,
                      enable: canClick)
                  .setContainer(
                      margin:
                          EdgeInsets.only(left: 60.w, right: 60.w, top: 26.w))
                  .setOnClickListener(() {
                sendAction();
              })
            ],
          ).setContainer(padding: EdgeInsets.symmetric(horizontal: 28.w))
        ],
      ).setContainer(color: Colors.white),
    );
  }

  void sendAction() {
    if (!canClick) return;
    if (textEditingController.text.isNotEmpty &&
        !textEditingController.text.contains("@")) {
      ToastUtil.showToast("請輸入正確電郵格式");
      return;
    }
    var params = {
      "email": textEditingController.text,
    };
    FocusManager.instance.primaryFocus?.unfocus();
    Obj.showLoading();
    ResponseBean result;
    Apis.forgetPwdByEmail(params)
        .then((response) => {
              Obj.dismissLoading(),
              result = ResponseBean.fromJson(response?.data ?? {}),
              if (result.isRequestSuccess)
                {
                  showDialog(),
                }
            })
        .catchError((error) => {Obj.dismissLoading(), setState(() {})});
  }

  void showDialog() {
    Get.dialog(
        SimpleDialog(
          titlePadding: EdgeInsets.zero,
          contentPadding: EdgeInsets.zero,
          insetPadding: EdgeInsets.symmetric(horizontal: 30.w),
          backgroundColor: Colors.transparent,
          children: [
            MyConfirmDialogWidget("密碼已重設並發送", "請以電郵內的新密碼登入帳號", "登入", () {
              Get.back();
              Get.back();
            })
          ],
        ),
        barrierDismissible: false,
        barrierColor: const Color.fromRGBO(0, 0, 0, 0.75));
  }
}
